package com.motong.gongdan.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.base.Strings;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

import static java.lang.Character.UnicodeBlock.*;

/**
 * @author:fff
 * @time:2022/3/14
 */
public class OwnUtil {
    public static void setInRedis(List<?> list, String key) {
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(list));
        RedisUtil.set(key, jsonArray);
    }

    public static void fileToResponse(File file, HttpServletResponse response) throws IOException {
        file.setWritable(true);
        file.setReadable(true);
        file.setExecutable(true);
        // Runtime.getRuntime().exec("chmod 777 "+file.getPath());
        OutputStream out = response.getOutputStream();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = br.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        br.close();
        out.close();
    }

    public static List<String> extractMessageByRegular(String msg) {
        List<String> list = new ArrayList<String>();
        if (!StringUtils.hasLength(msg)){
            return list;
        }
        int start = 0;
        int startFlag = 0;
        int endFlag = 0;
        for (int i = 0; i < msg.length(); i++) {
            if (msg.charAt(i) == '{') {
                startFlag++;
                if (startFlag == endFlag + 1) {
                    start = i;
                }
            } else if (msg.charAt(i) == '}') {
                endFlag++;
                if (endFlag == startFlag) {
                    list.add("$" + msg.substring(start, i + 1));
                }
            }
        }
        return list;
    }

    public static String readFileTxt(String fileName) {
        String str = "";
        ClassPathResource resource = new ClassPathResource(fileName);
        try {
            InputStream inputStream = resource.getInputStream();
            str = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("n"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static boolean checkStringContainChinese(String checkStr) {
        if (!Strings.isNullOrEmpty(checkStr)) {
            char[] checkChars = checkStr.toCharArray();
            for (int i = 0; i < checkChars.length; i++) {
                char checkChar = checkChars[i];
                if (checkCharContainChinese(checkChar)) {
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean checkCharContainChinese(char checkChar) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(checkChar);
        if (CJK_UNIFIED_IDEOGRAPHS == ub || CJK_COMPATIBILITY_IDEOGRAPHS == ub || CJK_COMPATIBILITY_FORMS == ub ||
                CJK_RADICALS_SUPPLEMENT == ub || CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A == ub || CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B == ub) {
            return true;
        }
        return false;
    }


    // 获取导入日志结果数据
    public static Map<String, Integer> getImportLogDataMap(Map<String, Integer> dataMap, Integer successNum, Integer failNum, Integer opType) {
        if (Objects.isNull(dataMap)) {
            dataMap = new HashMap<>(4);
        }
        dataMap.put("createSuccess", opType == 1 ? 0 : successNum);
        dataMap.put("createFail", opType == 1 ? 0 : failNum);
        dataMap.put("updateSuccess", opType != 1 ? 0 : successNum);
        dataMap.put("updateFail", opType != 1 ? 0 : failNum);
        return dataMap;
    }

    public static void main(String[] args) {
        String a = "${工单.编号}+${工单.产品}";
        System.out.println(extractMessageByRegular(a));
    }


}
